package com.css.msgcenter.admin.msglog.action;

import java.util.Calendar;
import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.css.db.page.Page;
import com.css.msgcenter.server.QueryMsg;
import com.css.msgcenter.server.model.MsgTaskLog;
import com.css.util.Messages;
import com.css.util.StringHelper;
import com.css.webbase.webwork.action.AdminAction;
import com.opensymphony.xwork.Action;

public class DirMsgTaskLog extends AdminAction{
	private static Log log = LogFactory.getLog(DirMsgTaskLog.class);
	private String sender;
	private String receiver;
	private String serverIp;
	private String msgStatus;
	private String taskId;
	private Date beginTime;
	private Date endTime;
	private Page page;
	
	public DirMsgTaskLog(){
		page = new Page();
		page.setCountField("a.uuid");
		result = Action.SUCCESS;
	}
	
	@Override
	protected String adminGo() {
		try{
			QueryMsg qm = new QueryMsg("select a.uuid from MsgTaskLog a " + getWhere() + getOrder());
			setWhere(qm);
			page = qm.page(page);
			page.setResults(QueryMsg.idToObj(MsgTaskLog.class,page.getResults()));
			return result;
		}catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			setMessage(Messages.getString("systemMsg.exception"));
			return Action.ERROR;
		}
	}
	
	public String getWhere() {
		StringBuffer sb = new StringBuffer(" where 1=1 ");
		if(StringHelper.isNotEmpty(taskId))
			sb.append(" and a.taskId = :taskId ");
		if(StringHelper.isNotEmpty(sender))
			sb.append(" and a.sender like :sender ");
		if(StringHelper.isNotEmpty(receiver))
			sb.append(" and a.receiver like :receiver ");
		if(StringHelper.isNotEmpty(serverIp))
			sb.append(" and a.serverIp like :serverIp ");
		if(StringHelper.isNotEmpty(msgStatus))
			sb.append(" and a.msgStatus =:msgStatus ");
		if(beginTime != null)
			sb.append(" and a.createTime >=:beginTime ");
		if(endTime != null)
			sb.append(" and a.createTime <:endTime ");
 		return sb.toString();
	}
	public void setWhere(QueryMsg qm) {
		if(StringHelper.isNotEmpty(taskId))
			qm.setParameter("taskId",taskId);
		if (StringHelper.isNotEmpty(sender))
			qm.setParameter("sender", "%" + sender.trim() + "%");
		if(StringHelper.isNotEmpty(receiver))
			qm.setParameter("receiver", "%" + receiver.trim() + "%");
		if(StringHelper.isNotEmpty(serverIp))
			qm.setParameter("serverIp","%" + serverIp + "%");
		if(StringHelper.isNotEmpty(msgStatus))
			qm.setParameter("msgStatus",msgStatus);
		if(beginTime != null)
			qm.setParameter("beginTime",beginTime);
		if(endTime!= null){
			Calendar cal = Calendar.getInstance();
	        cal.setTime(endTime);
	        cal.add(Calendar.DATE, 1);
	        qm.setParameter("endTime",cal.getTime());
		}
		
	}
	public String getOrder() {
		return StringHelper.isNotEmpty(page.getOrderByString()) ? page.getOrderByString() : " order by a.createTime desc ";
	}

	public String getTaskId() {
		return taskId;
	}

	public void setTaskId(String taskId) {
		this.taskId = taskId;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public String getSender() {
		return sender;
	}

	public void setSender(String sender) {
		this.sender = sender;
	}

	public String getReceiver() {
		return receiver;
	}

	public void setReceiver(String receiver) {
		this.receiver = receiver;
	}

	public String getServerIp() {
		return serverIp;
	}

	public void setServerIp(String serverIp) {
		this.serverIp = serverIp;
	}

	public Date getBeginTime() {
		return beginTime;
	}

	public void setBeginTime(Date beginTime) {
		this.beginTime = beginTime;
	}

	public Date getEndTime() {
		return endTime;
	}

	public void setEndTime(Date endTime) {
		this.endTime = endTime;
	}

	public String getMsgStatus() {
		return msgStatus;
	}

	public void setMsgStatus(String msgStatus) {
		this.msgStatus = msgStatus;
	}

}
